﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNH.MyLayouts;
using QLNH.QLNH_BUS_WS;
using System.Threading;

namespace QLNH.MyForms
{
    public partial class AdminRestaurantInfoEdit : FormUC
    {
        public AdminRestaurantInfoEdit(LayoutUC mainLayout)
        {
            InitializeComponent();
            this._mainLayout = mainLayout;
            this._formKey = FormKey.ADMIN_RESTAURANT_INFO_EDIT;
        }

        private void GetInfo()
        {
            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();
            InfoDTO info = ws.GetInfo();
            this.tb_name.BeginInvoke((MethodInvoker)(() => this.tb_name.Text = info.Name));
            this.tb_address.BeginInvoke((MethodInvoker)(() => this.tb_address.Text = info.Address));
            this.tb_phoneNumber.BeginInvoke((MethodInvoker)(() => this.tb_phoneNumber.Text = info.PhoneNumber));
            this.tb_description.BeginInvoke((MethodInvoker)(() => this.tb_description.Text = info.Description));
            this.bt_save.BeginInvoke((MethodInvoker)(() => this.bt_save.Enabled = false));
        }

        private void tb_TextChanged(object sender, EventArgs e)
        {
            int tag = Int32.Parse(((Control)sender).Tag.ToString());
            switch (tag)
            {
                case 0:
                    if (this.lb_nameWarning.Visible == true)
                        this.lb_nameWarning.Visible = false;
                    break;
                case 1:
                    if (this.lb_addressWarning.Visible == true)
                        this.lb_addressWarning.Visible = false;
                    break;
                case 2:
                    if (this.lb_phoneNumberWarning.Visible == true)
                        this.lb_phoneNumberWarning.Visible = false;
                    break;
                case 3:
                    if (this.lb_descWarning.Visible == true)
                        this.lb_descWarning.Visible = false;
                    break;
                default: 
                    break;
            }

            if (this.bt_save.Enabled == false)
                this.bt_save.Enabled = true;
        }

        private void AdminRestaurantInfoEdit_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(GetInfo));
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            if (this.tb_name.Text.Trim().Equals("") || this.tb_name.Text.Trim().Length > 100)
            {
                this.lb_nameWarning.Visible = true;
                return;
            }

            if (this.tb_address.Text.Trim().Equals("") || this.tb_address.Text.Trim().Length > 100)
            {
                this.lb_addressWarning.Visible = true;
                return;
            }

            if (this.tb_phoneNumber.Text.Trim().Equals("") || this.tb_phoneNumber.Text.Trim().Length > 20)
            {
                this.lb_phoneNumberWarning.Visible = true;
                return;
            }

            if (this.tb_description.Text.Trim().Equals("") || this.tb_description.Text.Trim().Length > 200)
            {
                this.lb_descWarning.Visible = true;
                return;
            }

            InfoDTO info = new InfoDTO();
            info.Name = this.tb_name.Text.Trim();
            info.Address = this.tb_address.Text.Trim();
            info.PhoneNumber = this.tb_phoneNumber.Text.Trim();
            info.Description = this.tb_description.Text.Trim();

            QLNH_BUS_WebServiceSoapClient ws = new QLNH_BUS_WebServiceSoapClient();

            if (ws.UpdateInfo(info) == true)
            {
                MessageBox.Show("Cập nhật thành công");
                ((LayoutAdmin)this._mainLayout).UpdateInfo();
                this._mainLayout.SwitchForm(FormKey.ADMIN_MAIN_FORM);
            }
            else
            {
                MessageBox.Show("Cập nhật thất bại");
            }
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            this._mainLayout.SwitchForm(FormKey.ADMIN_MAIN_FORM);
        }
    }
}
